package com.clothing.customization.controller.admin;

import com.clothing.customization.dto.ApiResult;
import com.clothing.customization.dto.UserDTO;
import com.clothing.customization.dto.UserQueryDTO;
import com.clothing.customization.service.UserManagementService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/api/admin/users")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class UserManagementController {
    
    private final UserManagementService userManagementService;
    
    /**
     * 分页查询用户列表
     */
    @GetMapping
    public ResponseEntity<ApiResult<Page<UserDTO>>> getUserList(UserQueryDTO queryDTO) {
        return ResponseEntity.ok(ApiResult.success(userManagementService.getUserList(queryDTO)));
    }
    
    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResult<UserDTO>> getUserDetail(@PathVariable Long id) {
        return ResponseEntity.ok(ApiResult.success(userManagementService.getUserDetail(id)));
    }
    
    /**
     * 创建用户
     */
    @PostMapping
    public ResponseEntity<ApiResult<UserDTO>> createUser(@Valid @RequestBody UserDTO userDTO) {
        return ResponseEntity.ok(ApiResult.success(userManagementService.createUser(userDTO)));
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResult<UserDTO>> updateUser(
            @PathVariable Long id,
            @Valid @RequestBody UserDTO userDTO) {
        return ResponseEntity.ok(ApiResult.success(userManagementService.updateUser(id, userDTO)));
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResult<Void>> deleteUser(@PathVariable Long id) {
        userManagementService.deleteUser(id);
        return ResponseEntity.ok(ApiResult.success());
    }
    
    /**
     * 修改用户状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<ApiResult<UserDTO>> updateUserStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        return ResponseEntity.ok(ApiResult.success(userManagementService.updateUserStatus(id, status)));
    }
    
    /**
     * 重置用户密码
     */
    @PutMapping("/{id}/password/reset")
    public ResponseEntity<ApiResult<Void>> resetPassword(
            @PathVariable Long id,
            @RequestParam String newPassword) {
        userManagementService.resetPassword(id, newPassword);
        return ResponseEntity.ok(ApiResult.success());
    }
} 